<?php
require_once("config/db.php");
// start a php array which we will return as ajax to our javascript (eventually)
if (isset($_POST['survey_id'])) {
 	$con=mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
	// Check connection
	if (mysqli_connect_errno()) {
	  	echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}

	$surveyId = $_POST['survey_id'];
	$jsonArray = array();

	$sql = 'SELECT DISTINCT entryId FROM surveyResults WHERE survey_id='.$surveyId ;
	
	$result = mysqli_query($con,$sql);
	$users;

	for ($i = 0; $row = mysqli_fetch_array($result); ++$i) {
		$entryId[$i] = $row['entryId'];
	}
	if (isset($entryId)) 
	{
		for ($j = 0; $j<count($entryId); ++$j) {
			$sql2 = 'SELECT user_name, weekDay, hour, minute, availability FROM surveyResults WHERE survey_id='.$surveyId.' AND entryId="'.$entryId[$j].'"';
	
			$result2 = mysqli_query($con,$sql2);
			$subArray = array();
			$user;
			while($row = mysqli_fetch_array($result2)) {
			$user = $row['user_name'];
			array_push($subArray, array('availability' => $row['availability'], 
							  'weekDay' => $row['weekDay'], 
							  'hour' => $row['hour'], 
							  'minute' => $row['minute']));
			}
			$jsonArray[$j]['user'] = $user;
			$jsonArray[$j]['entries'] = $subArray;
			
		}
		mysqli_close($con);
		echo json_encode($jsonArray);
		
	} else
	{
		mysqli_close($con);
		echo '';
	}

		

}
else
{echo "There's no survey lie that";}
?>
